<html>
<head><meta charset="utf-8"><title>Order of lifetime declarations matters? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html">Order of lifetime declarations matters?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="164042971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Order%20of%20lifetime%20declarations%20matters%3F/near/164042971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html#164042971">(Apr 24 2019 at 00:44)</a>:</h4>
<p>This code compiles:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Parser</span><span class="o">&lt;</span><span class="na">&#39;c</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;s</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">context</span>: <span class="kp">&amp;</span><span class="na">&#39;c</span><span class="w"> </span><span class="o">&amp;</span><span class="na">&#39;s</span><span class="w"> </span><span class="kt">str</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="na">&#39;c</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;s</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Parser</span><span class="o">&lt;</span><span class="na">&#39;c</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;s</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">parse</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="na">&#39;s</span><span class="w"> </span><span class="kt">str</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">context</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">parse_context</span><span class="o">&lt;</span><span class="na">&#39;s</span><span class="o">&gt;</span><span class="p">(</span><span class="n">context</span>: <span class="kp">&amp;</span><span class="na">&#39;s</span><span class="w"> </span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="na">&#39;s</span><span class="w"> </span><span class="kt">str</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Parser</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">context</span>: <span class="kp">&amp;</span><span class="nc">context</span><span class="w"> </span><span class="p">}.</span><span class="n">parse</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">parse_context</span><span class="p">(</span><span class="s">&quot;Available?&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>If you swap the <strong>declaration order</strong> of the lifetimes, it breaks:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Parser</span><span class="o">&lt;</span><span class="na">&#39;s</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;c</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span>error[E0515]: cannot return value referencing function parameter `context`
  --&gt; src/main.rs:12:5
   |
12 |     Parser { context: &amp;context }.parse()
   |     ^^^^^^^^^^^^^^^^^^--------^^^^^^^^^^
   |     |                 |
   |     |                 `context` is borrowed here
   |     returns a value referencing data owned by the current function
</pre></div>


<p>That's got to be a straight-up bug, yeah?</p>



<a name="164043061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Order%20of%20lifetime%20declarations%20matters%3F/near/164043061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html#164043061">(Apr 24 2019 at 00:46)</a>:</h4>
<p>I think so, yes. Probably worth filing an issue.</p>



<a name="164044579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Order%20of%20lifetime%20declarations%20matters%3F/near/164044579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html#164044579">(Apr 24 2019 at 01:20)</a>:</h4>
<p>Thanks! <a href="https://github.com/rust-lang/rust/issues/60219" target="_blank" title="https://github.com/rust-lang/rust/issues/60219">https://github.com/rust-lang/rust/issues/60219</a></p>



<a name="164071002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Order%20of%20lifetime%20declarations%20matters%3F/near/164071002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html#164071002">(Apr 24 2019 at 11:05)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> I'm not following <span class="user-mention" data-user-id="119031">@Esteban Küber</span>'s explanation; can you help me bridge the gap?</p>



<a name="164071079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Order%20of%20lifetime%20declarations%20matters%3F/near/164071079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html#164071079">(Apr 24 2019 at 11:06)</a>:</h4>
<p>Is it that it's effectively become</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="o">&lt;</span><span class="na">&#39;c</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;s</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Parser</span><span class="o">&lt;</span><span class="na">&#39;s</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;c</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="c1">// .                ^^^^^^ flipped</span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">parse</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="na">&#39;s</span><span class="w"> </span><span class="kt">str</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="c1">// .                    ^^ now the shorter lifetime</span>
</pre></div>



<a name="164071240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Order%20of%20lifetime%20declarations%20matters%3F/near/164071240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> davidtwco <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html#164071240">(Apr 24 2019 at 11:09)</a>:</h4>
<p>That aligns with my understanding of the comment.</p>



<a name="164072156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Order%20of%20lifetime%20declarations%20matters%3F/near/164072156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Order.20of.20lifetime.20declarations.20matters.3F.html#164072156">(Apr 24 2019 at 11:26)</a>:</h4>
<p>Thank you!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>